package com.ruoyi.web.controller.datastatistics;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.basedata.domain.MeterSheets;
import com.ruoyi.basedata.service.IMeterSheetsService;
import com.ruoyi.datastatistics.service.IElectricityTerminalsFreezeXbService;
import com.ruoyi.datastatistics.vo.DbHourUsageVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.basedata.domain.ElectricityAreaReport;
import com.ruoyi.basedata.service.IElectricityAreaReportService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 区域能耗报表Controller
 *
 * @author gdqt
 * @date 2025-03-10
 */
@RestController
@RequestMapping("/energy/report")
public class ElectricityAreaReportController extends BaseController
{
    @Autowired
    private IElectricityAreaReportService electricityAreaReportService;

    @Autowired
    private IElectricityTerminalsFreezeXbService electricityTerminalsFreezeXbService;

    @Autowired
    private IMeterSheetsService meterSheetsService;
    /**
     * 查询区域能耗报表列表
     */

    @GetMapping("/list")
    public TableDataInfo list(ElectricityAreaReport electricityAreaReport)
    {
        startPage();
        List<ElectricityAreaReport> list = electricityAreaReportService.selectElectricityAreaReportList(electricityAreaReport);
        MeterSheets meterSheets = new MeterSheets();
        meterSheets.setIds(electricityAreaReport.getAreaIds());
        List<MeterSheets> arealist= meterSheetsService.selectMeterSheetsList(meterSheets);
        list.forEach(e->{
            String areaName = arealist.stream().filter(a->a.getId().equals(e.getAreaId())).map(MeterSheets::getSheetsName).collect(Collectors.toList()).get(0);
            e.setAreaName(areaName);
        });
        return getDataTable(list);
    }

    /**
     * 导出区域能耗报列表
     */

    @Log(title = "能耗分析", businessType = BusinessType.EXPORT)
    @PostMapping("/export2")
    public void export2(HttpServletResponse response, ElectricityAreaReport electricityAreaReport)
    {
        electricityAreaReportService.exportElectricityAreaReportList2(response,electricityAreaReport);
    }
    /**
     * 导出区域能耗报列表
     */

    @Log(title = "区域能耗报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ElectricityAreaReport electricityAreaReport)
    {
        electricityAreaReportService.exportElectricityAreaReportList(response,electricityAreaReport);
    }

    /**
     * 获取区域能耗报表详细信息
     */

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(electricityAreaReportService.selectElectricityAreaReportById(id));
    }

    /**
     * 新增区域能耗报表
     */

    @Log(title = "区域能耗报表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ElectricityAreaReport electricityAreaReport)
    {
        return toAjax(electricityAreaReportService.insertElectricityAreaReport(electricityAreaReport));
    }

    /**
     * 修改区域能耗报表
     */

    @Log(title = "区域能耗报表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ElectricityAreaReport electricityAreaReport)
    {
        return toAjax(electricityAreaReportService.updateElectricityAreaReport(electricityAreaReport));
    }

    /**
     * 删除区域能耗报表
     */
    @Log(title = "区域能耗报表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(electricityAreaReportService.deleteElectricityAreaReportByIds(ids));
    }


    /**
    * @Description: 获取能耗报表
    * @date: 2025/3/10 16:20
    * @author: zt
    */
    @GetMapping("/getEnergyReportList")
    public AjaxResult getEnergyReportList(ElectricityAreaReport electricityAreaReport)
    {
        Map<String,Object> resMap = electricityAreaReportService.getEnergyReportList(electricityAreaReport);
        return AjaxResult.success(resMap);
    }
    /**
     * @Description: 获取能耗分析报表
     * @date: 2025/3/10 16:20
     * @author: kyx
     */
    @GetMapping("/getEnergyReportListForAnalysis")
    public AjaxResult getEnergyReportListForAnalysis(ElectricityAreaReport electricityAreaReport)
    {
        Map<String,Object> resMap = electricityAreaReportService.getEnergyReportListForAnalysis(electricityAreaReport);
        return AjaxResult.success(resMap);
    }


    /*---------------------------------能耗概况--------------------------------------*/
    /**
    * @Description: 能耗概况-获取磁贴中各类数据
    * @date: 2025/3/13 10:45
    * @author: zt
    */
    @GetMapping("/getSummarizeList")
    public AjaxResult getSummarizeList(){
        List<Object> resList = electricityAreaReportService.getSummarizeList();
        return AjaxResult.success(resList);
    }

    /**
    * @Description: 能耗概况-当月园区线损情况
    * @date: 2025/3/13 15:33
    * @author: zt
    */
    @GetMapping("/getLineLossMonthList")
    public AjaxResult getLineLossMonthList(){
        Map<String,Object> resMap = electricityAreaReportService.getLineLossMonthList();
        return AjaxResult.success(resMap);
    }

    /**
     * @Description: 能耗概况-当日园区总负荷
     * @date: 2025/3/13 15:33
     * @author: zt
     */
    @GetMapping("/getEnergyDayList")
    public AjaxResult getEnergyDayList(){
        Map<String,Object> resMap = electricityAreaReportService.getEnergyDayList();
        return AjaxResult.success(resMap);
    }


    /**
    * @Description: 能耗概况-电表用量前3数据
    * @date: 2025/3/14 14:22
    * @author: zt
    */
    @GetMapping("/getTopFlowList")
    public AjaxResult getTopFlowList(){
        Map<String,Object> resList = electricityTerminalsFreezeXbService.getTopFlowList();
        return AjaxResult.success(resList);
    }
    /*---------------------------------能耗概况--------------------------------------*/
}
